/*
 * $Id: ArticleTag.java 16 2012-01-12 09:30:13Z rook.alex@gmail.com $
 */

package ua.org.energy.vkek.site.model;

import com.google.appengine.api.datastore.Key;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;

/**
 * Tag is a some Label for Aricle for filtering purposes
 */

@NamedQueries({
    @NamedQuery(name = "ArticleTag.findAll", 
                query = "SELECT obj from ArticleTag obj"),
    @NamedQuery(name = "ArticleTag.findByName", 
                query = "SELECT obj from ArticleTag obj WHERE obj.tagName=:tagName"),
    @NamedQuery(name="ArticleTag.findCount",
                query="SELECT count(obj) from ArticleTag obj"),
    @NamedQuery(name="ArticleTag.findArticlesByTag",
                query="SELECT obj from Article obj WHERE obj.tag=:tag")
})

@Entity
public class ArticleTag implements Serializable,BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key ident;

    //@Column(unique = true) //UnsupportedOperationException: No support for uniqueness constraints :-(
    private String tagName;
    
    //A some 'Weight' of Tag for using in tag-cloud
    private int tagWeight;
    

    @ManyToOne(optional=false)
    private ArticleCategory category;
    
    
    @OneToMany(mappedBy="tag")
    private List<Article> articles;
    
    /**
     * @return the tagName
     */
    public String getTagName() {
        return tagName;
    }

    /**
     * @param tagName the tagName to set
     */
    public void setTagName(String tagName) {
        this.tagName = tagName;
    }

    /**
     * @return the tagWeight
     */
    public int getTagWeight() {
        return tagWeight;
    }

    /**
     * @param tagWeight the Weight to set
     */
    public void setTagWeight(int tagWeight) {
        this.tagWeight = tagWeight;
    }

    public int incWeight(){
        tagWeight++;
        return tagWeight;
           
    }
    
    public int decWeght(){
        if (tagWeight>0) 
            tagWeight--;
        return tagWeight;
    }
    /**
     * @return the category
     */
    public ArticleCategory getCategory() {
        return category;
    }

    /**
     * @param ArticleCategory the category to set
     */
    public void setCategory(ArticleCategory category) {
        this.category = category;
    }

    /**
     * @return the ident
     */
    public Key getIdent() {
        return ident;
    }

    /**
     * @return the articles
     */
    public List<Article> getArticles() {
        return articles;
    }

    /**
     * @param articles the articles to set
     */
    public void setArticles(List<Article> articles) {
        this.articles = articles;
    }

    /**
     * @param ident the ident to set
     */
    public void setIdent(Key ident) {
        this.ident = ident;
    }

    @Override
    public ArticleTag safeClone() {
      ArticleTag buf=new ArticleTag();
      buf.setIdent(ident);
      buf.setTagName(tagName);
      buf.setTagWeight(tagWeight);
      return buf;
    }

}
